package com.freak.neteasecloudmusic.modules.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.circularbeadimageview.CircularBeadClipPathImageView;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.app.App;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.base.adapter.MainTabAdapter;
import com.freak.neteasecloudmusic.modules.mine.entity.MinePlayListEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MinePurchasedEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineRadioStationEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineRecordEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineUserDetailEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineUserInfoEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.PersonFmEntity;
import com.freak.neteasecloudmusic.modules.mine.fragment.MySongListFragment;
import com.freak.neteasecloudmusic.modules.mine.fragment.adapter.MyMusicAdapter;
import com.freak.neteasecloudmusic.utils.SPUtils;
import com.freak.neteasecloudmusic.utils.ToastUtil;
import com.freak.neteasecloudmusic.view.custom.viewpager.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseAbstractMvpFragment<MinePresenter> implements MineContract.View {
    @BindView(R.id.recyclerViewMineMy)
    RecyclerView recyclerViewMineMy;
    @BindView(R.id.textViewSongListDetailContext)
    TextView textViewSongListDetailContext;
    @BindView(R.id.textViewSongListDetailAuthor)
    TextView textViewSongListDetailAuthor;
    @BindView(R.id.textViewMineGrade)
    TextView textViewMineGrade;
    @BindView(R.id.textViewSongListDetailDetail)
    TextView textViewSongListDetailDetail;
    @BindView(R.id.viewMine)
    View viewMine;
    @BindView(R.id.imageViewMineLocalMusic)
    ImageView imageViewMineLocalMusic;
    @BindView(R.id.textViewMineLocalMusic)
    TextView textViewMineLocalMusic;
    @BindView(R.id.constraintLayoutMineLocalMusic)
    ConstraintLayout constraintLayoutMineLocalMusic;
    @BindView(R.id.imageViewMineDownManager)
    ImageView imageViewMineDownManager;
    @BindView(R.id.textViewMineDownManager)
    TextView textViewMineDownManager;
    @BindView(R.id.constraintLayoutMineDownManager)
    ConstraintLayout constraintLayoutMineDownManager;
    @BindView(R.id.imageViewMineRadioStation)
    ImageView imageViewMineRadioStation;
    @BindView(R.id.textViewMineRadioStation)
    TextView textViewMineRadioStation;
    @BindView(R.id.constraintLayoutMineRadioStation)
    ConstraintLayout constraintLayoutMineRadioStation;
    @BindView(R.id.imageViewMineCollect)
    ImageView imageViewMineCollect;
    @BindView(R.id.textViewMineCollect)
    TextView textViewMineCollect;
    @BindView(R.id.constraintLayoutMineCollect)
    ConstraintLayout constraintLayoutMineCollect;
    @BindView(R.id.imageViewMineNewSong)
    ImageView imageViewMineNewSong;
    @BindView(R.id.textViewMineNewSong)
    TextView textViewMineNewSong;
    @BindView(R.id.constraintLayoutMineNewSong)
    ConstraintLayout constraintLayoutMineNewSong;
    @BindView(R.id.imageViewMinePhoto)
    CircularBeadClipPathImageView imageViewMinePhoto;
    @BindView(R.id.textViewMineMyMusic)
    TextView textViewMineMyMusic;
    @BindView(R.id.textViewMineSinger)
    TextView textViewMineSinger;
    @BindView(R.id.viewMineArrow)
    View viewMineArrow;
    @BindView(R.id.coordinatorLayoutSongListDetail)
    CoordinatorLayout coordinatorLayoutSongListDetail;
    @BindView(R.id.textViewMineLatelyPlay)
    TextView textViewMineLatelyPlay;
    @BindView(R.id.viewMinePlayArrow)
    View viewMinePlayArrow;
    @BindView(R.id.constraintLayoutPlayMore)
    ConstraintLayout constraintLayoutPlayMore;
    @BindView(R.id.circularBeadClipPathImageViewSong)
    CircularBeadClipPathImageView circularBeadClipPathImageViewSong;
    @BindView(R.id.textViewSongPlayCount)
    TextView textViewSongPlayCount;
    @BindView(R.id.constraintLayoutMineSong)
    ConstraintLayout constraintLayoutMineSong;
    @BindView(R.id.circularBeadClipPathImageViewImage)
    CircularBeadClipPathImageView circularBeadClipPathImageViewImage;
    @BindView(R.id.textViewMineVideoContext)
    TextView textViewMineVideoContext;
    @BindView(R.id.constraintLayoutMineVideo)
    ConstraintLayout constraintLayoutMineVideo;
    @BindView(R.id.textViewMineCreateSongListTip)
    TextView textViewMineCreateSongListTip;
    @BindView(R.id.textViewMineCreateSongList)
    TextView textViewMineCreateSongList;
    @BindView(R.id.constraintLayoutMineCreateSongList)
    ConstraintLayout constraintLayoutMineCreateSongList;
    @BindView(R.id.textViewMineCreateCollectSongListTip)
    TextView textViewMineCreateCollectSongListTip;
    @BindView(R.id.textViewMineCollectSongListList)
    TextView textViewMineCollectSongListList;
    @BindView(R.id.constraintLayoutMineCollectSongList)
    ConstraintLayout constraintLayoutMineCollectSongList;
    @BindView(R.id.imageViewSongListMore)
    ImageView imageViewSongListMore;
    @BindView(R.id.customViewPagerMineSongList)
    CustomViewPager customViewPagerMineSongList;
    private MySongListFragment createSongListFragment;
    private MySongListFragment collectSongListFragment;
    private List<Fragment> mFragments;
    private List<String> tabTitles;
    private MainTabAdapter mainTabAdapter;
    private MyMusicAdapter mMyMusicAdapter;
    private List<MultiItemEntity> mMusicList;

    @Override
    protected MinePresenter createPresenter() {
        return new MinePresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initEventAndData() {
        mPresenter.getPlaylist((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""));
        mPresenter.getUserDetail((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""));
        mPresenter.getRecord((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""), "0");
    }

    @Override
    protected void initView(View view) {
        mFragments = new ArrayList<>();
        tabTitles = new ArrayList<>();
        tabTitles.add("创建歌单");
        tabTitles.add("收藏歌单");
        Bundle bundle = new Bundle();
        bundle.putInt("type", 0);
        mMusicList = new ArrayList<>();
        mMyMusicAdapter = new MyMusicAdapter(mMusicList, mActivity);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewMineMy.setLayoutManager(linearLayoutManager);
        mMyMusicAdapter.bindToRecyclerView(recyclerViewMineMy);
        //添加自定义分割线：可自定义分割线高度和颜色
        recyclerViewMineMy.setAdapter(mMyMusicAdapter);
        createSongListFragment = new MySongListFragment();
        createSongListFragment.setArguments(bundle);
        collectSongListFragment = new MySongListFragment();
        Bundle collectBundle = new Bundle();
        collectBundle.putInt("type", 1);
        collectSongListFragment.setArguments(collectBundle);
        mFragments.add(createSongListFragment);
        mFragments.add(collectSongListFragment);
        mainTabAdapter = new MainTabAdapter(getChildFragmentManager(), mFragments, tabTitles);
        customViewPagerMineSongList.setAdapter(mainTabAdapter);
        customViewPagerMineSongList.setOffscreenPageLimit(4);
        setSelect(0);
    }

    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void showToast(String toast) {
        ToastUtil.shortShow(toast);
    }

    /**
     * 用户详情
     *
     * @param mineUserInfoEntity
     */
    @Override
    public void getUserInfoSuccess(MineUserInfoEntity mineUserInfoEntity) {

    }

    /**
     * 用户信息，歌单，收藏，mv,dj数量
     *
     * @param model
     */
    @Override
    public void getUserDetailSuccess(MineUserDetailEntity model) {
        if (model != null) {
            if (model.getProfile() != null) {
                Glide.with(mActivity).load(model.getProfile().getAvatarUrl()).thumbnail(0.1f).into(imageViewMinePhoto);
                textViewSongListDetailContext.setText(model.getProfile().getNickname());
            }
        }
    }

    /**
     * 播放列表
     *
     * @param model
     */
    @Override
    public void getPlaylistSuccess(MinePlayListEntity model) {
        if (model != null) {
            createSongListFragment.getPlaylistSuccess(model);
            collectSongListFragment.getPlaylistSuccess(model);
            int createCount = 0;
            int collectCount = 0;
            if (model.getPlaylist() != null && model.getPlaylist().size() > 0) {
                for (int i = 0; i < model.getPlaylist().size(); i++) {
                    if (i == 0) {
                        mMusicList.add( model.getPlaylist().get(i));
                    } else {
                        if (model.getPlaylist().get(i).isSubscribed()) {
                            collectCount++;
                        } else {
                            createCount++;
                        }
                    }
                }
            }
            textViewMineCreateSongList.setText(String.valueOf(createCount));
            textViewMineCollectSongListList.setText(String.valueOf(collectCount));
            mMyMusicAdapter.notifyDataSetChanged();
        }
        mPresenter.getPersonFm();
    }

    /**
     * 获取用户电台
     *
     * @param model
     */
    @Override
    public void getUserRadioStationSuccess(MineRadioStationEntity model) {

    }

    /**
     * 数字专辑
     *
     * @param model
     */
    @Override
    public void getPurchasedSuccess(MinePurchasedEntity model) {

    }

    /**
     * 获取用户播放记录
     *
     * @param model
     */
    @Override
    public void getRecordSuccess(MineRecordEntity model) {
        if (model != null) {
            if (model.getAllData() != null && model.getAllData().size() > 0) {
                textViewSongPlayCount.setText(model.getAllData().size() + "首");
                Glide.with(mActivity).load(model.getAllData().get(0).getSong().getAl().getPicUrl()).thumbnail(0.1f).into(circularBeadClipPathImageViewSong);
            }
        }
    }

    @Override
    public void getPersonFmSuccess(PersonFmEntity model) {
        if (model != null) {
            if (model.getData() != null && model.getData().size() > 0) {
                mMusicList.add( model.getData().get(0));
            }
        }
        mMyMusicAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.constraintLayoutMineCollectSongList, R.id.constraintLayoutMineCreateSongList, R.id.constraintLayoutMineLocalMusic, R.id.constraintLayoutMineDownManager, R.id.constraintLayoutMineRadioStation, R.id.constraintLayoutMineCollect, R.id.constraintLayoutMineNewSong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.constraintLayoutMineLocalMusic:
                mPresenter.getUserLikeList((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""));

                break;
            case R.id.constraintLayoutMineDownManager:

                break;
            case R.id.constraintLayoutMineRadioStation:
                mPresenter.getUserRadioStation((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""));

                break;
            case R.id.constraintLayoutMineCollect:
                mPresenter.getUserInfo();
                mPresenter.getPurchased(10);
                break;
            case R.id.constraintLayoutMineNewSong:

                break;
            case R.id.constraintLayoutMineCreateSongList:
                setSelect(0);
                break;
            case R.id.constraintLayoutMineCollectSongList:
                setSelect(1);
                break;
        }
    }

    public void setSelect(int type) {
        customViewPagerMineSongList.setCurrentItem(type);
        setSelectTab(type);
    }

    private void setSelectTab(int i) {
        switch (i) {
            case 0:
                constraintLayoutMineCreateSongList.setSelected(true);
                constraintLayoutMineCollectSongList.setSelected(false);
                break;
            case 1:
                constraintLayoutMineCreateSongList.setSelected(false);
                constraintLayoutMineCollectSongList.setSelected(true);
                break;
            default:
                break;
        }
    }
}
